<!-- 商品列表 -->
<template>
  <div class="goods-list">
    <van-sticky >
      <van-tabs v-model="activeName" @click="changeTab" >
        <van-tab
          v-for="item in navData"
          :key="item.id"
          :title="item.name"
          :name="item.name"
        ></van-tab>
      </van-tabs>
    </van-sticky>

    <div class="goods-list-name">
      <div class="category-name">{{ currentNav.name }}</div>
      <div class="front-desc">{{ currentNav.front_desc }}</div>
    </div>
    <ul v-if="goodsList.length > 0" class="goods-list-container">
      <li class="item" v-for="item in goodsList" :key="item.id" @click="goGoodsDetail(item.id)">
        <img class="img" :src="item.list_pic_url" alt="" />
        <div class="goods-name">{{ item.name }}</div>
        <div class="goods-price">{{ item.retail_price }}</div>
      </li>
    </ul>
    <div v-else>数组库暂无数据</div>
  </div>
</template>

<script>
import {
  goodsList as goodsListApi,
  categoryNav as categoryNavApi
} from '@/api/category'
export default {
  name: 'GoodsList',
  data() {
    return {
      activeName: '',
      categoryId: '', // 分类id
      goodsList: [], //商品列表
      currentNav: {}, // 当前分类
      navData: [] // 分类列表
    }
  },

  components: {},

  computed: {},

  created() {
    // console.log(this.$route.query.id);
    this.categoryId = this.$route.query.id
    this.activeName = this.$route.query.name
    this.getGoodsList()
    this.getNavData()
  },

  mounted() {},

  methods: {
    changeTab(name, title) {
      // console.log(name,title);
      this.categoryId = this.navData.find((item) => item.name == name).id
      this.getGoodsList()
    },
    async getGoodsList() {
      let res = await goodsListApi({ categoryId: this.categoryId })
      this.goodsList = res.data
      this.currentNav = res.currentNav
    },
    async getNavData() {
      let res = await categoryNavApi({ id: this.categoryId })
      this.navData = res.navData
    },
    goGoodsDetail(id){
        // this.$router.push({name:'GoodsDetail',query:{id}})
        this.$router.push({name:'GoodsDetail',params:{id}})
    }
  }
}
</script>
<style lang='scss' scoped>
.goods-list {
  height: 100vh;
  overflow: auto;
  background-color: #f4f4f4;
  &-name {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 100px;
    background-color: #fff;
    .category-name {
      font-size: 18px;
    }
    .front-desc {
      font-size: 12px;
      color: #999;
    }
  }
  &-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    margin-top: 5px;
    .item {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      margin-bottom: 10px;
      width: 49%;
      background-color: #fff;
      .img {
        width: 150px;
        height: 150px;
      }
      .goods-name {
      }
      .goods-price {
        color: red;
        font-size: 16px;
      }
    }
  }
}
</style>